﻿# For Program Demo

#=== Tool 指定编译工具路径 ==== 
 
ToolPath=../../Tool/seh/mingw64/bin
CC := $(ToolPath)/gcc.exe 
AS :=  $(ToolPath)/gcc.exe -x assembler-with-cpp
CCFLAG += -DCFG_TC_MINGW

# ToolPath=./Tool/tcc
# CC := $(ToolPath)/tcc.exe 
# AS := $(ToolPath)/tcc.exe
# CCFLAG += -DCFG_TC_TCC

CLEANUP =rm -f

#Step2 设置编译参数 : 按实际添加

CFLAGS += -Wall
CCFLAG += -g

# 增加宏定义 HAVE_CONFIG_H
CCFLAG += -DHAVE_CONFIG_H


DBGFLAG := -g
CCOBJFLAG := $(CCFLAG) -c

#Step3 设置主要源目录路径 及 输出路径
# 1-顶层 关键文件夹定义
# 2-输出目标定义
BIN_PATH := .
OBJ_PATH := Obj
SRC_PATH := Src
ASM_PATH := Src
 
TARGET_NAME := Demo
TARGET := $(BIN_PATH)/$(TARGET_NAME)

#=== 连接脚本参数 ====
# LDSCRIPT = xxx.ld
# LFLAGS :=
# LFLAGS= -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map
# Note: Tcc 工具链不能单独开启此项
# LFLAGS = -Wl,-Map=$(TARGET).map


#=== 添加库搜索路径
DLL_PATH := 
# DLL_PATH = -LD:/XXX/mingw64/lib
# DLL_PATH = -LD:/XXX/mingw64/x86_64-w64-mingw32/lib

#=== 添加对应的库
DLL_LIB :=
# DLL_LIB = -lmingw32 
# DLL_LIB += -llibwinmm
DLL_LIB += -lwinmm
DLL_LIB += -lUser32
DLL_LIB += -lKernel32

#=== 需要添加对应的SDL库 .lib 到tcc 
# DLL_LIB += -lSDL2main

# 添加此库
# DLL_LIB += -lSDL2

# -lOpengl32
# -lglu32 \
# -lglut 
			  

#Step7 收集源码, 方式1：手动设置

# 手动配置形式：设置 源文件路径
# R_DIR := $(SRC_PATH)
# SRC_PATH+=$(R_DIR)/XXX
# SRC_PATH+=$(R_DIR)/YYY
# SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))

#Step7 收集源码, 方式2：自动递归遍历
# 自动递归式
rwildcard=$(foreach d,$(wildcard $(addsuffix *,$(1))),$(call rwildcard,$(d)/,$(2))$(filter $(subst *,%,$(2)),$(d)))

SRC := $(call rwildcard,$(SRC_PATH),*.c)

#Step8 一些基本处理和配置 : 固定不需要修改
ASM_SRC := $(wildcard $(ASM_PATH)/*.s)
SRC += $(ASM_SRC)
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

DIR_PATH := $(SRC_PATH)/
DIR_PATH += $(sort  $(dir $(basename $(SRC))))

INCLUDE_PATH += $(addprefix -I ,$(DIR_PATH))

vpath %.c $(DIR_PATH)
vpath %.s $(DIR_PATH)

$(OBJ_PATH)/%.o: %.c
	$(CC) $(CCOBJFLAG) $(INCLUDE_PATH) -o $@ $<

$(OBJ_PATH)/%.o: %.s
	$(AS) -c $(CCOBJFLAG) $< -o $@
 
#=== Execute Rule ==== 
default: start all


.PHONY: start
start:
	@echo Start:

#=== 
.PHONY: show
show:
	@echo $(SRC)
	@echo $(OBJ)
	@echo $(INCLUDE_PATH)
	@echo $(DIR_PATH)
	
# phony rules
.PHONY: all 
all: $(TARGET).exe
	@echo Done

# 1-核心规则
$(TARGET).exe: $(OBJ)
	$(CC) $(CCFLAG) $(INCLUDE_PATH) $(OBJ) $(DLL_PATH) $(DLL_LIB) $(LFLAGS) -o $@ 

#=== Clean Files List ==== 
CLEAN_LIST := 
# CLEAN_LIST += $(TARGET).elf
# CLEAN_LIST += $(TARGET).hex 
# CLEAN_LIST += $(TARGET).bin 
CLEAN_LIST += $(TARGET).map 
CLEAN_LIST += $(TARGET).exe
CLEAN_LIST += $(OBJ)			  

.PHONY: clean
clean:
	@echo CLEAN $(CLEAN_LIST)
	$(CLEANUP) $(CLEAN_LIST)
	

